﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HFController
{
    /// <summary>
    /// 广告业务逻辑
    /// </summary>
    public class AdController:ControllerBase
    {
        //本类单列对象
        private static AdController _adController;
        //单列模式实例化
        public static AdController getController()
        {
            if (_adController == null)
            {
                _adController = new AdController();
            }
            return _adController;
        }
        #region 广告分类

        /// <summary>
        /// 广告分类数据库访问
        /// </summary>
        private hf.DAL.AdClass AdClassDAL = new hf.DAL.AdClass();

        /// <summary>
        /// 添加广告分类
        /// </summary>
        /// <param name="AdClassName"></param>
        public void doAddAdClass(string AdClassName)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.AdClass model = new hf.Model.AdClass();

            model.AdClassName = AdClassName;

            try
            {
                model.ID = AdClassDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='AdClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到广告类列表，用作绑定下拉框
        /// </summary>
        /// <param name="LoadCount">读取记录数</param>
        /// <returns></returns>
        public List<hf.Model.AdClass> getAdClassList(int LoadCount)
        {
            List<hf.Model.AdClass> list = new List<hf.Model.AdClass>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = AdClassDAL.GetAdClassList(LoadCount, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 根据一个ID 得到一条广告分类
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.AdClass getAdClassModel(int ID)
        {
            hf.Model.AdClass model = new hf.Model.AdClass();
            int total;

            List<hf.Model.AdClass> list = AdClassDAL.GetAdClassList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 修改广告分类
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="AdClassName"></param>
        public void doModifyAdClass(int ID, string AdClassName)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.AdClass model = this.getAdClassModel(ID);
            model.AdClassName = AdClassName;

            try
            {
                //执行修改
                AdClassDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='AdClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
        /// <summary>
        /// 得到广告分类列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.AdClass> getAdClassList(out string PagerStr)
        {
            List<hf.Model.AdClass> list = new List<hf.Model.AdClass>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = AdClassDAL.GetAdClassList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 删除广告分类
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeAdClass(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                AdClassDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='AdClass.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        } 
        #endregion

        #region 广告

        /// <summary>
        /// 广告数据库访问
        /// </summary>
        private hf.DAL.Ad AdDAL = new hf.DAL.Ad();

        /// <summary>
        /// 添加广告
        /// </summary>
        /// <param name="TypeID"></param>
        /// <param name="AdClassID"></param>
        /// <param name="Title"></param>
        /// <param name="PicUrl"></param>
        /// <param name="HttpUrl"></param>
        public void doAddAd(string TypeID, string AdClassID, string Title, string PicUrl, string HttpUrl)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Ad model = new hf.Model.Ad();

            model.TypeID = int.Parse(TypeID);
            model.AdClassID = int.Parse(AdClassID);
            model.Title=Title;
            model.PicUrl = PicUrl;
            model.HttpUrl = HttpUrl;

            try
            {
                model.ID = AdDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='Ad.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 根据ID 得到一条广告
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.Ad getAdModel(int ID)
        {
            hf.Model.Ad model = new hf.Model.Ad();

            int total;
            List<hf.Model.Ad> list = AdDAL.GetAdList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 修改广告
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="TypeID"></param>
        /// <param name="AdClassID"></param>
        /// <param name="Title"></param>
        /// <param name="PicUrl"></param>
        /// <param name="HttpUrl"></param>
        public void doModifyAd(int ID, string TypeID, string AdClassID, string Title, string PicUrl, string HttpUrl)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Ad model = this.getAdModel(ID);

            model.TypeID = int.Parse(TypeID);
            model.AdClassID = int.Parse(AdClassID);
            model.Title = Title;
            model.PicUrl = PicUrl;
            model.HttpUrl = HttpUrl;

            try
            {
                //执行修改
                AdDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='AdList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 得到广告列表
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="TypeID"></param>
        /// <param name="AdClassID"></param>
        /// <returns></returns>
        public List<hf.Model.Ad> getAdList(int Count, int TypeID, int AdClassID)
        {
            List<hf.Model.Ad> list = new List<hf.Model.Ad>();
            //当前页码
            int PageNumber = 1;
            //总数据记录
            int total;

            //筛选条件
            string strSql = "TypeID=" + TypeID.ToString() + " and AdClassID=" + AdClassID.ToString();

            //获得列表
            list = AdDAL.GetAdList(Count, PageNumber, strSql, out total);
            return list;
        }
        /// <summary>
        /// 得到广告列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.Ad> getAdList(out string PagerStr)
        {
            List<hf.Model.Ad> list = new List<hf.Model.Ad>();

            //每页显示的记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }

            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = AdDAL.GetAdList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 删除广告
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeAd(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
               AdDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='AdList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDS"></param>
        public void doDelegeAd(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    AdDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='AdList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
        #endregion
    }
}
